const myDb = require('../../../libs/mydb');

module.exports = {

    //收入支出渲染 返回用户资产详细表ID、添加类型，金额，支出或收入，消费备注，添加时间，
    incomepay_render: async function (req, res) {
        let { uid } = req.query;
        await myDb.singleSearch('ac_money_tb,ac_type'
            , 'ac_money_tb.mo_id,ac_type.type_name,ac_money_tb.mo_money,ac_money_tb.mo_flage,ac_money_tb.con_rex,ac_money_tb.con_time'
            , `WHERE ac_money_tb.expen_type_id=ac_type.type_id AND ac_money_tb.uid=${uid} ORDER BY mo_id DESC`)
            .then(
                result => {
                    if (result) {
                        res.send({
                            code: 200,
                            msg: 'success',
                            incomepayData: result
                        })
                    } else {
                        res.send({
                            code: 400,
                            msg: 'fail',
                        })
                    }

                }
            )
    },

    //收入支出添加  data里传用户id,收入或者支出类型，消费类型名称，消费时间，消费备注，消费金额
    incomepay_add: async function (req, res) {
        let data = req.body;
        await myDb.singleSearch('ac_type', 'type_id', `WHERE type_name='${data.expen_type_id}'`)
            .then(
                result => {
                    result.forEach(element => {
                        data.expen_type_id = element.type_id
                    });
                }
            )

        await myDb.add('ac_money_tb', data, '')
            .then(
                result => {
                    res.send({
                        code: 200,
                        msg: 'success'
                    })

                }
            )
    },

    //收入/支出当月金额 传当月月份 如2021/11 传收入或支出类型
    incomepay_money: async function (req, res) {
        console.log(req.query);
        let { datatime, flage, uid } = req.query;
        console.log(datatime, flage, uid)
        await myDb.singleSearch('ac_money_tb', 'SUM(mo_money) AS money'
            , `WHERE con_time LIKE '${datatime}%' AND mo_flage = ${flage} AND uid=${uid}`)
            .then(
                result => {
                    console.log(result);
                    res.send({
                        code: 200,
                        msg: 'success',
                        moneyData: result
                    })
                }
            )
    },

    //收入支出的删除 传用户资产详细表ID
    incomepay_del: async function (req, res) {
        let { moid } = req.body;
        if (moid == '') {
            res.send({
                code: 400,
                msg: 'Data cannot be empty'
            });
        }
        await myDb.del('ac_money_tb', `WHERE ac_money_tb.mo_id = ${moid}`)
            .then(
                result => {
                    res.send({
                        code: 200,
                        msg: 'Delete succeeded'
                    });
                }
            )
    },

    /**
     * 支出收入类别查询
     * @param {*} req 
     * @param {*} res 
     */
    show_type: function (req, res) {

        let { flag } = req.query
        // 支出
        if (flag == 0) {
            myDb.singleSearch('ac_type', 'type_id,type_name,type_uid',
                ` WHERE type_id BETWEEN 1 AND 8 `)
                .then(
                    result => {
                        if (result) {
                            res.send({
                                code:200,
                                msg: 'ok',
                                tpyedata: result
                            })
                        }else {
                            res.send ({
                                code: 400,
                                msg : 'NOt FADE'
                            })
                        }
                    }
                )

        }
        if (flag == 1) {
            myDb.singleSearch('ac_type', 'type_id,type_name,type_uid',
            ` WHERE type_id BETWEEN 9 AND 19 `)
            .then(
                result => {
                    if (result) {
                        res.send({
                            code:200,
                            msg: 'ok',
                            tpyedata: result
                        })
                    }else {
                        res.send ({
                            code: 400,
                            msg : 'NOt FADE'
                        })
                    }
                }
            )

        }
    }














}